package com.sx.controller;

import com.sx.pojo.OptionRecords;
import com.sx.pojo.Person;
import com.sx.service.OptionRecordService;
import com.sx.service.PersonService;
import com.sx.service.serviceImpl.OptionRecordServiceImpl;
import com.sx.service.serviceImpl.PersonServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/optionRecord.action")
public class OptionRecord extends HttpServlet {
    private OptionRecordService optionRecordService = new OptionRecordServiceImpl();
    private PersonService personService = new PersonServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        String action = req.getParameter("action");
        switch (action) {
            case "list" -> {
                ArrayList<OptionRecords> optionRecordsList = optionRecordService.getAllOptionRecord();
                ArrayList<Person> personList = personService.getAllPerson();
                // 处理列表请求，重置筛选条件
                //List<OptionRecords> optionRecordsList = optionRecordService.getOptionRecords(null, 1, 10); // 默认第一页，每页 10 条记录
                req.setAttribute("operatorList", personList);
                req.setAttribute("optionRecordsList", optionRecordsList);
                req.setAttribute("mainRight", "/jsp/optionRecordList.jsp");
                req.getRequestDispatcher("/jsp/index.jsp").forward(req, resp);
            }
            /*case "delete" -> {
                String id = req.getParameter("id");
                if (id != null) {
                    // 这里假设你会调用相应的服务层方法进行人员删除操作
                    // 例如：PersonService.deletePerson(id);
                    // 这里为了示例，模拟删除操作成功或失败
                    boolean success = optionRecordService.deleteOptionRecordById(id);
                    if (success) {
                        // 删除成功，返回成功消息
                        resp.getWriter().println("{\"status\":\"success\",\"message\":\"操作记录删除成功\"}");
                    } else {
                        // 删除失败，返回失败信息和原因
                        resp.getWriter().println("{\"status\":\"error\",\"message\":\"操作记录删除失败，可能是因为关联数据存在，请先处理关联数据。\"}");
                    }
                }
            }*/
           /* case "listN" -> {
                String startDate = req.getParameter("startDate");
                String endDate = req.getParameter("endDate");
                String operatorId = req.getParameter("operatorName");
                String operationType = req.getParameter("operationType");
                String tableType = req.getParameter("tableType");
                resp.setContentType("application/json;charset=UTF-8");
                ArrayList<Person> personList = personService.getAllPerson();
                req.setAttribute("operatorList", personList);
                List<OptionRecords> optionRecordsList = optionRecordService.getFilteredOptionRecords(startDate, endDate, operatorId, operationType, tableType);
                req.setAttribute("optionRecordsList", optionRecordsList);
                req.setAttribute("mainRight", "/jsp/optionRecordList.jsp");
                req.getRequestDispatcher("/jsp/index.jsp").forward(req, resp);
            }*/
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String action = req.getParameter("action");
        String startDate = req.getParameter("startDate");
        String endDate = req.getParameter("endDate");
        String operatorId = req.getParameter("operatorName");
        String operationType = req.getParameter("operationType");
        String tableType = req.getParameter("tableType");
        resp.setContentType("application/json;charset=UTF-8");
        if (action.equals("listN")) {
            // 处理筛选请求
            //OptionRecords filter = new OptionRecords();
            List<OptionRecords> optionRecordsList = optionRecordService.getFilteredOptionRecords(startDate, endDate, operatorId, operationType, tableType);
            req.setAttribute("optionRecordsList", optionRecordsList);
            ArrayList<Person> personList = personService.getAllPerson();
            req.setAttribute("operatorList", personList);
            req.setAttribute("mainRight", "/jsp/optionRecordList.jsp");
            req.getRequestDispatcher("/jsp/index.jsp").forward(req, resp);
        }
    }


}
